/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sgct.Avaliador;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Hyperlink;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import com.sun.webui.jsf.model.Option;
import java.util.ArrayList;
import java.util.Collection;
import javax.faces.FacesException;
import Controle.*;
import Dados.Avaliacao;
import Dados.Avaliador;
import java.lang.Float;
import javax.faces.convert.BigIntegerConverter;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.ServletContext;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version AreaAvaliador.java
 * @version Created on 30/05/2009, 09:04:13
 * @author clock
 */

public class AreaAvaliador extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private SingleSelectOptionsList dropDown1DefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getDropDown1DefaultOptions() {
        return dropDown1DefaultOptions;
    }

    public void setDropDown1DefaultOptions(SingleSelectOptionsList ssol) {
        this.dropDown1DefaultOptions = ssol;
    }
    private DropDown dropDown1 = new DropDown();

    public DropDown getDropDown1() {
        return dropDown1;
    }

    public void setDropDown1(DropDown dd) {
        this.dropDown1 = dd;
    }
    private TextField nota = new TextField();

    public TextField getNota() {
        return nota;
    }

    public void setNota(TextField tf) {
        this.nota = tf;
    }
    private TextArea comment = new TextArea();

    public TextArea getComment() {
        return comment;
    }

    public void setComment(TextArea ta) {
        this.comment = ta;
    }
    private Button button1 = new Button();

    public Button getButton1() {
        return button1;
    }

    public void setButton1(Button b) {
        this.button1 = b;
    }
    private Hyperlink download1 = new Hyperlink();

    public Hyperlink getDownload1() {
        return download1;
    }

    public void setDownload1(Hyperlink h) {
        this.download1 = h;
    }
    private BigIntegerConverter bigIntegerConverter1 = new BigIntegerConverter();

    public BigIntegerConverter getBigIntegerConverter1() {
        return bigIntegerConverter1;
    }

    public void setBigIntegerConverter1(BigIntegerConverter bic) {
        this.bigIntegerConverter1 = bic;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public AreaAvaliador() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized

        // Carrega o DropDown com os artigos do avaliador
        Avaliador aval = (Avaliador) Controle.GetInstance().getUsuarioAtual();
        Collection<Avaliacao> listaAvaliacao = aval.GetAppraisals();

        Collection<Option> itens = new ArrayList();
        itens.add(new Option(null,"Selecione um Artigo"));

        for (Avaliacao f : listaAvaliacao)
        {
            String nome = f.GetArticle().GetTitle();
            itens.add(new Option(nome,nome));
        }

        this.comment.setDisabled(true);
        this.nota.setDisabled(true);
        this.button1.setDisabled(true);
        this.download1.setVisible(false);

        dropDown1.setItems(itens);

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("AreaAvaliador Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    public String sair_action() {
        // TODO: Processe a ação. O valor de retorno é um nome de caso
        // de navegação em que nulo retornará à mesma página.
        return "sair";
    }

    public String avaliar_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        Float fNota = new Float(-1);
        //verifica se e um float valido
        try
        {
            fNota = Float.parseFloat(this.getNota().getText().toString());
        }
        catch(Exception ex)
        {
            error("Valor de Nota Invalido");
            info("Entre com um valor numerico");
            return null;
        }

        //verifica se esta entre 0 e 10
        if(fNota < 0 || fNota > 10)
        {
            error("Valor de Nota Invalido");
            info("Valor deve estar entre 0 e 10");
            return null;
        }
        
        String titulo = this.getDropDown1().getSelected().toString();
        Avaliacao aval = ((Avaliador) Controle.GetInstance().getUsuarioAtual()).buscaAvaliacao(titulo);
        aval.setComentario(this.getComment().getText().toString());
        aval.setNota(new Float(this.getNota().getText().toString()));

        error("Sucesso");
        info("Avaliaçao submetida com sucesso");
        return null;
    }

    public String dropDown1_processValueChange(ValueChangeEvent vce) {
        if(!this.getDropDown1().getSelected().toString().equals(""))
        {
            String titulo = this.getDropDown1().getSelected().toString();
            Avaliacao aval = ((Avaliador) Controle.GetInstance().getUsuarioAtual()).buscaAvaliacao(titulo);

            this.comment.setDisabled(false);
            this.nota.setDisabled(false);
            this.button1.setDisabled(false);
            this.download1.setVisible(true);

            this.comment.setText(aval.GetComment());
            this.nota.setText(""+aval.GetGrade());

            ServletContext theApplicationsServletContext = (ServletContext) getExternalContext().getContext();
            String DestinationPath = "/sgct/resources/artigosSubmetidos/" +dropDown1.getSelected().toString() + ".pdf";
            download1.setUrl(DestinationPath);
            return "";
        }else
        {
        this.comment.setDisabled(true);
        this.nota.setDisabled(true);
        this.button1.setDisabled(true);
        this.download1.setVisible(false);
        this.comment.setText(null);
        this.nota.setText(null);
        return "";
        }
    }

}
